package com.semlab.server.enrichment;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.net.URL;
import java.net.URLEncoder;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.semlab.server.enrichment.http.HttpManager;
import com.semlab.server.resources.Concept;
import com.semlab.server.resources.SLog;
import com.semlab.shared.NamespaceConstants;

import javax.imageio.ImageIO;

@Component
public class IMDB {

    private static final String SERVICE_URL = "http://www.omdbapi.com/";
    private static final String IMDB_URL = "http://www.imdb.com/title/";

    @Autowired
    HttpManager httpmanager;

    public Concept updateMovieImdb(Concept concept) {
        // String result = getImdbUri(concept.getName());
        // if(result != null){
        // concept.addProperty(NamespaceConstants.IMDB_URI, result);
        // SLog.debug(getClass(), "Found imdb uri: "+result);
        // }
        String response;
        try {
            response = httpmanager.executeGet(SERVICE_URL + "?i=&t="
                    + URLEncoder.encode(concept.getName(), "UTF-8"));
            JSONObject object = new JSONObject(response);
            if (object.has("Title")) {
                String ititle = object.getString("Title");
                if (ititle.equalsIgnoreCase(concept.getName())) {
                    String id = object.getString("imdbID");
                    concept.addProperty(NamespaceConstants.IMDB_URI, IMDB_URL
                            + id);
                }
            }
            if (object.has("Poster")) {
                String poster = object.getString("Poster");
                try {
//                    System.out.println("poster="+poster);
                    BufferedImage bi = ImageIO.read(new URL(poster));
                    String image = "";
                    java.io.ByteArrayOutputStream os= new java.io.ByteArrayOutputStream();
                    ImageIO.write(bi, "jpg", os);
                    byte[] data= os.toByteArray();
                    image = new sun.misc.BASE64Encoder().encode(data);
                    concept.addProperty(NamespaceConstants.IMDB_POSTER_64, image);
//                    System.out.println("image="+image);
                } catch (Exception e) {
                    System.out.println("image parsing error");
                }

            }
            if (object.has("imdbRating")) {
                String rating = object.getString("imdbRating");
                concept.addProperty(NamespaceConstants.IMDB_RATING, rating);
            }

            if (object.has("Genre")) {
                String genre = object.getString("Genre");
                concept.addProperty(NamespaceConstants.IMDB_GENRE, genre);
            }

            if (object.has("Plot")) {
                String plot = object.getString("Plot");
                concept.addProperty(NamespaceConstants.IMDB_PLOT, plot);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return concept;
    }

//	public String getImdbUri(String title) {
//		String response;
//		try {
//			response = httpmanager.executeGet(SERVICE_URL + "?i=&t="
//					+ URLEncoder.encode(title, "UTF-8"));
//			JSONObject object = new JSONObject(response);
//			if (object.has("Title")) {
//				String ititle = object.getString("Title");
//				if (ititle.equalsIgnoreCase(title)) {
//					String id = object.getString("imdbID");
//					return IMDB_URL + id;
//				}
//			}
//
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return null;
//	}

}
